package _03_栈;

import java.util.Stack;

public class _150_逆波兰表达式求值 {

    public static void main(String[] args) {
        String[] tokens = new String[]{"10","6","9","3","+","-11","*","/","*","17","+","5","+"};

        System.out.println(evalRPN(tokens));

    }


    public static int evalRPN(String[] tokens) {
        Stack<Integer> list = new Stack<>();
        for (String cur : tokens) {
            if ("+".equals(cur) ||
                    "-".equals(cur) ||
                    "*".equals(cur) ||
                    "/".equals(cur)) {
                // 表达式
                Integer last = list.pop();
                Integer first = list.pop();
                switch (cur) {
                    case "+":
                        last = first + last;
                        break;
                    case "-":
                        last = first - last;
                        break;
                    case "*":
                        last = first * last;
                        break;
                    case "/":
                        last = first / last;
                        break;
                }
                list.push(last);
            } else {
                // 数值
                list.push(Integer.parseInt(cur));
            }
        }
        return list.pop();
    }
}
